package com.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.EmployeeBean;
import com.project.bean.VoildAdd;
import com.project.bean.VoildUpdate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @RequestMapping("reg")
    public String reg(@Validated(VoildAdd.class) EmployeeBean em, BindingResult result) throws Exception {

        //判断验证是否通过，如果没有通过，则返回true，通过返回false
        if(result.hasErrors()){
            //result.getFieldErrors()得到验证错误集合。
            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
        }
        System.out.println(em);
        return "ok";
    }

    @RequestMapping("update")
    public String update(@Validated(VoildUpdate.class) EmployeeBean em, BindingResult result) throws Exception {
        //判断验证是否通过，如果没有通过，则返回true，通过返回false
        if(result.hasErrors()){
            //result.getFieldErrors()得到验证错误集合。
            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
        }
        System.out.println(em);
        return "ok";
    }
}
